Skip to content

Add support for custom scripts - #903

Open
Lucca-mito wants to merge 4 commits into
swiftlang:mainfrom
Lucca-mito:main
Open

Lucca-mito wants to merge 4 commits into
swiftlang:mainfrom
Lucca-mito:main

Conversation

@Lucca-mito

@Lucca-mito Lucca-mito commented Oct 13, 2024 •

Copy link
Copy Markdown

Summary

First-class support for adding custom scripts to a DocC-generated website. These may be local scripts, in which case the website will continue to work offline, or they may be external scripts at a user-specified URL. This support is in the form of a custom-scripts.json file, the scripting analog of theme-settings.json.

Full pitch: https://forums.swift.org/t/pitch-support-for-custom-scripts-in-docc/75357.

Dependencies

Corresponding change in swift-docc.

Testing

  • Unit tests added.
  • To test the feature end-to-end, you'll need to use the version of swift-docc with the corresponding changes. Follow the steps in the pitch for adding any custom scripts you want to your documentation website (your own scripts, or a library such as MathJax or D2). If you want a specific example to test the implementation:
    • Copy the custom-scripts.json and the custom mathjax-config.js script (shown in the pitch) to your documentation catalog.
    • Add LaTeX expressions to your in-source documentation.
    • docc convert the documentation catalog with your custom scripts. Observe that the modified swift-docc copied custom-scripts.json and the custom scripts into the documentation archive.
    • Preview the documentation archive. Observe that the LaTeX expressions are rendered on the page.
  • Live demo: see here and here. The LaTeX expressions in the documentation text are dynamically rendered into equations via custom scripts.

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests
  • Ran npm test, and it succeeded
  • Updated documentation if necessary
    • Existing documentation does not need to change. New documentation for this feature will be added after the design and implementation are finalized.

@mportiz08 mportiz08 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool to see this in action, and I know this kind of customization will be super useful to some developers. Nice work!

I'm very interested to hear other feedback from the community on your forums pitch.

Here's some initial feedback on the renderer implementation itself in the meantime.

Comment thread src/App.vue Outdated
Comment thread src/utils/custom-scripts.js Outdated
Comment thread src/utils/fetch-text.js Outdated
Comment thread src/utils/object-properties.js Outdated
Comment thread src/utils/custom-scripts.js Outdated
Comment thread src/utils/custom-scripts.js Outdated
@Lucca-mito

Copy link
Copy Markdown
Author

@swift-ci test

@Lucca-mito
Lucca-mito requested a review from mportiz08 November 11, 2025 03:36
`runCustomNavigateScripts` returned as soon as it had added the script elements, without waiting for the scripts to run, and an invalid custom script stopped the remaining scripts from running. Wait for each navigation script to run or fail to load before resolving, and report invalid scripts and scripts that fail to load in the console instead of letting them prevent the remaining scripts from running, both on page load and on navigation.
The renderer requests custom-scripts.json on page load and on each navigation, which results in a 404 for documentation without custom scripts. Include an empty custom-scripts.json in the template, like theme-settings.json, so that the request always succeeds. DocC replaces it with the catalog's custom-scripts.json, if there is one.
The renderer requested custom-scripts.json on page load and again on every navigation. Fetch it once and reuse the result, like theme-settings.json. If the request fails, the next routing event tries again.
@Lucca-mito

Copy link
Copy Markdown
Author

Update: rebased onto main. Changes:

  • No more eval/new Function: navigation scripts are re-run by adding a fresh script element, which removes itself once the script has run. This improves CSP support since, now, custom scripts never need unsafe-eval or unsafe-inline.
  • Removed inline scripts (code) and defer: inline scripts would need unsafe-inline, and defer has no effect on dynamically added scripts.
  • One invalid or failing script no longer blocks the others.
  • Scripts are guaranteed to run only after the page’s content is rendered: they’re now triggered from newContentMounted() instead of a router hook.
  • No more 404s: the template now ships an empty custom-scripts.json, like theme-settings.json (Add empty theme-settings.json to template output #1001).

Companion DocC PR: swiftlang/swift-docc#1056. See my latest comment in that PR for a link to a live demo of custom scripts with the latest changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants